/**
* @Author: ${.Author}
* @Email: ${.Email}
* @Description:
* @File:  ${.TplTableName}_controller
* @Version: 1.0.0
* @Date: ${.DateTime}
*/

package controller


import (
   "${.TplProject}/utils/base"
   "${.TplProject}/module/${.moduleName}/model"
   "github.com/gogf/gf/frame/g"
   "github.com/gogf/gf/net/ghttp"
   "github.com/gogf/gf/os/glog"
   "github.com/gogf/gf/util/gconv"
)

type ${.TplModelName}Controller struct {
    base.BaseRouter
}

var (
    controllerName${.TplModelName} = "${.TplModelName}Controller"
)

//${.TplModelName}页面信息
func (controller *${.TplModelName}Controller) Index(r *ghttp.Request) {
    base.WriteTpl(r, "${.pathName}.html", g.Map{})
}

//获取${.TplModelName}单条信息
func (controller *${.TplModelName}Controller) Get(r *ghttp.Request) {
    id := r.GetInt("id")
    model := model.${.TplModelName}{Id: id}.Get()
    if model.Id <= 0 {
       base.Fail(r, controllerName${.TplModelName}+" get fail")
    }
    base.Succ(r, model)
}

//根据id或者ids删除${.TplModelName}
func (controller *${.TplModelName}Controller) Delete(r *ghttp.Request) {
   ids := r.GetInts("ids")
    for _, id := range ids {
        model := model.${.TplModelName}{Id: id}
        model.Delete()
    }
    base.Succ(r, "")
}


//创建一条${.TplModelName}
func (controller *${.TplModelName}Controller) Save(r *ghttp.Request) {
    model := model.${.TplModelName}{}
    err := gconv.Struct(r.GetPostMap(), &model)
    if err != nil {
      glog.Error( controllerName${.TplModelName}+" save struct error", err)
      base.Error(r, "save error")
    }
    var num int64
    if model.Id <= 0 {
      num = model.Insert()
    } else {
      num = model.Update()
    }

    if num <= 0 {
       base.Fail(r, controllerName${.TplModelName}+" save fail")
    }

    base.Succ(r, "")
}

//更新一条${.TplModelName}
func (controller *${.TplModelName}Controller) Update(r *ghttp.Request) {
    model := model.${.TplModelName}{}
    err := gconv.Struct(r.GetPostMap(), &model)
    if err != nil {
    glog.Error( controllerName${.TplModelName}+" save struct error", err)
    base.Error(r, "save error")
    }
    var num int64
    if model.Id <= 0 {
    num = model.Insert()
    } else {
    num = model.Update()
    }

    if num <= 0 {
    base.Fail(r, controllerName${.TplModelName}+" save fail")
    }

    base.Succ(r, "")
}

//分页列表${.TplModelName}
func (controller *${.TplModelName}Controller) Page(r *ghttp.Request) {
   form := base.NewForm(r.GetQueryMap())
   model := model.${.TplModelName}{}
   page := model.Page(&form)
   base.Succ(r, g.Map{"list": page, "form": form})
}
